package com.study.happystudy.controller;

import com.study.happystudy.entity.ResultEntity;
import com.study.happystudy.po.Activity;
import com.study.happystudy.po.User;
import com.study.happystudy.service.impl.ActivityServiceImpl;
import com.study.happystudy.service.impl.MessageServiceImpl;
import com.study.happystudy.service.impl.PointTransactionServiceImpl;
import com.study.happystudy.service.impl.UserServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("activity/")
public class ActivityController {

    @Resource
    ActivityServiceImpl activityService;
    @Resource
    UserServiceImpl userService;
    @Resource
    MessageServiceImpl messageService;

    @Resource
    PointTransactionServiceImpl pointTransactionService;

    @PostMapping("addActivity")
    public String addActivity(@RequestParam String title,
                              @RequestParam String imageUrl,
                              @RequestParam String content,
                              @RequestParam String startTime,
                              @RequestParam String endTime) {
        activityService.addActivity(title, imageUrl, content, startTime, endTime);
        return ResultEntity.Success(1, "添加成功");
    }

    @DeleteMapping("deleteActivityById")
    public String deleteActivityById(@RequestParam int activityId) {
        if(activityService.getActivityById(activityId) == null){
            return ResultEntity.Fail(-1, "活动不存在");
        }
        activityService.deleteActivityById(activityId);
        return ResultEntity.Success(1, "删除成功");
    }

    @PutMapping("updateActivity")
    public String updateActivity(@RequestParam String title,
                                 @RequestParam String imageUrl,
                                 @RequestParam String content,
                                 @RequestParam String startTime,
                                 @RequestParam String endTime,@RequestParam int activityId) {

        if(activityService.getActivityById(activityId) == null){
            return ResultEntity.Fail(-1, "活动不存在");
        }
        activityService.updateActivity(title, imageUrl, content, startTime, endTime,activityId);
        return ResultEntity.Success(1, "修改成功");
    }

    @GetMapping("getActivityById")
    public Activity getActivityById(@RequestParam int activityId) {
        return activityService.getActivityById(activityId);
    }

    @GetMapping("getUsersByActivityId")
    public List<User> getUsersByActivityId(@RequestParam int activityId) {
        return activityService.getUsersByActivityId(activityId);
    }

    @GetMapping("getActivities")
    public Object getAllActivities(@RequestParam String type) {
        return switch (type) {
            case "全部" -> activityService.getAllActivities();
            case "未开始" -> activityService.getUnstartedActivities();
            case "进行中" -> activityService.getOngoingActivities();
            case "已结束" -> activityService.getFinishedActivities();
            default -> ResultEntity.Fail(-1, "参数错误");
        };
    }
    @PostMapping("addPoint")
    public String addPoint(@RequestParam int userId, @RequestParam int activityId,@RequestParam int points,@RequestParam String reason) {

        if(activityService.isUserInActivity(userId, activityId) == 0){
            return ResultEntity.Fail(-1, "用户未参加该活动");
        }

        User user = userService.findUserById(userId);
        userService.updatePoints(userId,user.getStudentPoints()+points);

        pointTransactionService.insertPointTransaction(userId,points,reason);

        messageService.sendActivityMessage(activityId,userId,"活动积分奖励"+points+"分");
        return ResultEntity.Success(1, "添加积分成功");
    }

    @PostMapping("addUserToActivity")
    public String addUserToActivity(@RequestParam int userId, @RequestParam int activityId) {
        if (activityService.isUserInActivity(userId, activityId) == 1) {
            return ResultEntity.Fail(-1, "用户已参加该活动");
        }
        activityService.addUserToActivity(userId, activityId);
        Activity activity = activityService.getActivityById(activityId);
        String activityTitle = "\""+activity.getTitle()+"\"";
        messageService.sendActivityMessage(activityId,userId,"您已参加"+activityTitle+"活动");
        return ResultEntity.Success(1, "添加成功");
    }
}
